<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2008 by CPG-Nuke Dev Team
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version
**********************************************/
if (!defined('CPG_NUKE')) { exit; }

Dragonfly_Output_Css::add('cpgmm');

global $prefix, $db, $module_name, $currentlang, $mainindex, $adminindex, $MAIN_CFG;

$menucats = array();
$modquery = $lnkquery = '';
$setstatus = 1;

if (!is_admin())
{
	$modquery = 'WHERE m.active=1 AND m.inmenu=1';
	$lnkquery = 'WHERE l.active=1';
	$view = array();
	$view[] = 0;
	if (is_user()) {
		$view[] = 1;
		foreach (Dragonfly::getKernel()->IDENTITY->groups AS $key => $value) {
			$view[] = $key+3;
		}
	} else {
		$view[] = 3;
	}
	$modquery .= ' AND m.view IN ('.implode(',', $view).')';
	$lnkquery .= ' AND l.view IN ('.implode(',', $view).')';
}

// Load active modules from database
$sql = 'SELECT m.title as link, m.custom_title as title, m.view, m.active, m.inmenu, m.cat_id, m.pos AS linkpos, c.name, c.image, c.pos AS catpos, c.link AS catlnk, c.link_type AS cattype FROM '.$prefix.'_modules AS m LEFT JOIN '.$prefix."_modules_cat c ON (c.cid = m.cat_id) $modquery";
$result = $db->query($sql);
while ($row = $result->fetch_assoc())
{
	if ($row['title'] == '') {
			$row['title'] = (defined("_$row[link]LANG")) ? constant("_$row[link]LANG") : str_replace('_', ' ', $row['link']);
	}
	$row['link_type'] = -1;
	if (!isset($row['catpos'])) {
		$row['catpos'] = -1;
	}
	$menucats[$row['catpos']][$row['linkpos']] = $row;
}
$result->free();

// Load custom links from database
$sql = "SELECT l.title, l.link, l.link_type, l.view, l.active, l.cat_id, l.pos AS linkpos, c.name, c.image, c.pos AS catpos, c.link AS catlnk, c.link_type AS cattype FROM ".$prefix."_modules_links AS l LEFT JOIN ".$prefix."_modules_cat c ON (c.cid = l.cat_id) $lnkquery";
$result = $db->query($sql);
while ($row = $result->fetch_assoc())
{
	if (defined($row['title'])) {
		$row['title'] = constant($row['title']);
	}
	$link = str_ireplace('&amp;', '&', $row['link']);
	if (URL::uri() != '') {
		if (false !== strpos(URL::uri(), $link)) {
			$row['status'] = 'current';
			$setstatus = 0;
		}
	}
	$row['link']   = str_ireplace('&', '&amp;', $link);
	$row['catlnk'] = str_ireplace('&', '&amp;', $row['catlnk']);
	$row['inmenu'] = 1;
	if (!isset($row['catpos'])) {
		$row['catpos'] = -1;
	}
	$menucats[$row['catpos']][$row['linkpos']] = $row;
}
$result->free();

$nocatcontent = '';
$content = "\n".'<ul id="menuList">';

ksort($menucats);
while (list($ccat, $items) = each($menucats))
{
	ksort($items);
	$catcontent = $offcontent = $hidcontent = '';
	while (list($dummy, $item) = each($items))
	{
		$status = '';
		if ($setstatus && $item['link'] == $module_name) {
			$status = 'current';
			$setstatus = 0;
		}
		if (!$item['active']) {
			$status = 'inactive';
		} elseif ($item['active'] && !$item['inmenu']) {
			$status = 'disabled';
		}
		$status = isset($item['status']) ? $item['status'] : $status;

		if ($item['link_type'] <= 0) {
			$item['link'] = URL::index($item['link']);
		} elseif ($item['link_type'] == 2) {
			$item['link'] .= '" target="_blank';
		}

		$tmpcontent = '<li class="'.$status.'"><a href="'.$item['link'].'">'.$item['title'].'</a></li>';
		if (!$item['active'] && !$item['inmenu']) {
			$offcontent .= $tmpcontent;
		} elseif (!$item['active']) {
			$hidcontent .= $tmpcontent;
		} else {
			$catcontent .= $tmpcontent;
		}
		$cattitle = $item['name'];
		$catlnk  = $item['catlnk'];
		$cattype = $item['cattype'];
	}

	$cattitle = (defined($cattitle) ? constant($cattitle) : $cattitle);
	$catcontent .= $hidcontent.$offcontent;
	if (!empty($catlnk)) {
		if ($cattype <= 0) {
			$catlnk = URL::index($catlnk);
		} elseif ($cattype == 2) {
			$catlnk .= '" target="_blank';
		}
		$cattitlel = '<a href="'.$catlnk.'">'.$cattitle.'</a>';
	} else {
		$cattitlel = '<a onclick="">'.$cattitle.'</a>';
	}
	if ($ccat >= 0) {
		$content .= "<li>{$cattitlel}\n <ul>".$catcontent;
		$content .= "</ul></li>";
	} else {
		$nocatcontent = "<li><a>None</a>\n <ul>".$catcontent;
		$nocatcontent .= "</ul></li>";
	}
}
$content .= $nocatcontent;

if (is_admin())
{
	global $CLASS, $cpgtpl;
	//Dragonfly_Admin_Menu::display('all', Dragonfly_Admin_Menu::CSS);
	$content .= '<li><a href="'.$adminindex.'"><b>&raquo;</b> '._ADMIN.' <b>&laquo;</b></a>';
	//$content .= $cpgtpl->S_ADMIN_CSSMENU;
	$content .='</li>';
}

$content .= "\n</ul>\n";
$mmcontent = $content;
